package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
)

type optionManageController struct {
}

func (e *optionManageController) init() {
	httpserver.Server.POST("/option/data", e.selectDataList)
	httpserver.Server.POST("/option/tree", e.selectTreeList)
}

func (e *optionManageController) selectDataList(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	mode := Map.GetString(body, "mode", "")

	if Method.HasEmpty(mode) {
		return nil, errors.New("参数不能为空：mode")
	}
	if "Option1" == mode {
		return e.selectOption1(body)
	}
	if "Option2" == mode {
		return e.selectOption2(body)
	}
	if "Option3" == mode {
		return e.selectOption3(body)
	}
	if "Option4" == mode {
		return e.selectOption4(body)
	}

	return nil, errors.New("不支持的操作模式")
}

func (e *optionManageController) selectOption1(body map[string]interface{}) (interface{}, error) {
	entityType := Map.GetString(body, "entityType", "")
	camelField := Map.GetString(body, "field", "")

	if Method.HasEmpty(entityType, camelField) {
		return nil, errors.New("参数不能为空：entityType, field")
	}

	// 其次查询单参数
	underField := String.CamelToUnderline(camelField)

	entity, err := commEntity.NewEntity(entityType)
	if err != nil {
		return nil, err
	}
	if Method.HasEmpty(entity.TableName()) {
		return nil, errors.New("表名称为空d")
	}

	// 查询数据
	mapList, err := commSql.Fox.SelectOption1List(entity.TableName(), underField, true)
	if err != nil {
		return nil, err
	}

	return e.getOptionList(mapList, camelField, camelField), nil
}

func (e *optionManageController) selectOption2(body map[string]interface{}) (interface{}, error) {
	entityType := Map.GetString(body, "entityType", "")
	camelField := Map.GetString(body, "field", "")
	value := Map.GetString(body, "value", "")
	camelField2 := Map.GetString(body, "field2", "")

	if Method.HasEmpty(entityType, camelField, value, camelField2) {
		return nil, errors.New("参数不能为空：entityType, field, value, field2")
	}

	// 其次查询单参数
	underField1 := String.CamelToUnderline(camelField)
	underField2 := String.CamelToUnderline(camelField2)

	entity, err := commEntity.NewEntity(entityType)
	if err != nil {
		return nil, err
	}
	if Method.HasEmpty(entity.TableName()) {
		return nil, errors.New("表名称为空d")
	}

	// 查询数据
	mapList, err := commSql.Fox.SelectOption2List(entity.TableName(), underField1, underField2, value, true)
	if err != nil {
		return nil, err
	}

	// 用field1/field2作为distinct的条件，value1作为筛选条件
	return e.getOptionList(mapList, camelField2, camelField2), nil
}

func (e *optionManageController) selectOption3(body map[string]interface{}) (interface{}, error) {
	entityType := Map.GetString(body, "entityType", "")
	camelField1 := Map.GetString(body, "field1", "")
	value1 := Map.GetString(body, "value1", "")
	camelField2 := Map.GetString(body, "field2", "")
	value := Map.GetString(body, "value", "")
	label := Map.GetString(body, "label", "")

	if Method.HasEmpty(entityType, camelField1, value1, camelField2) {
		return nil, errors.New("参数不能为空：entityType, camelField1, value1, camelField2 ")
	}

	// 其次查询单参数
	underField1 := String.CamelToUnderline(camelField1)
	underField2 := String.CamelToUnderline(camelField2)
	underValue := String.CamelToUnderline(value)

	entity, err := commEntity.NewEntity(entityType)
	if err != nil {
		return nil, err
	}
	if Method.HasEmpty(entity.TableName()) {
		return nil, errors.New("表名称为空d")
	}

	// 查询数据
	mapList, err := commSql.Fox.SelectOptionList3(entity.TableName(), underField1, underField2, underValue, value1, true)
	if err != nil {
		return nil, err
	}

	return e.getOptionList(mapList, value, label), nil
}

func (e *optionManageController) selectOption4(body map[string]interface{}) (interface{}, error) {
	entityType := Map.GetString(body, "entityType", "")
	field := Map.GetString(body, "field", "")
	filters := Map.GetSlice(body, "filters", make([]interface{}, 0))

	if Method.HasEmpty(entityType, field, filters) {
		return nil, errors.New("参数不能为空：entityType, field, filters")
	}

	// 其次查询单参数
	underField := String.CamelToUnderline(field)

	where := make(map[string]interface{})
	for _, v := range filters {
		filter := v.(map[string]interface{})

		val, ok := filter["field"]
		if !ok {
			continue
		}

		filterField := String.CamelToUnderline(val.(string))
		where[filterField] = filter["value"]
	}

	entity, err := commEntity.NewEntity(entityType)
	if err != nil {
		return nil, err
	}
	if Method.HasEmpty(entity.TableName()) {
		return nil, errors.New("表名称为空d")
	}

	// 查询数据
	mapList, err := commSql.Fox.SelectOptionList(entity.TableName(), underField, where, true)
	if err != nil {
		return nil, err
	}

	return e.getOptionList(mapList, field, field), nil
}

func (e *optionManageController) getOptionList(mapList []map[string]interface{}, valueField string, labelField string) []map[string]interface{} {
	result := make([]map[string]interface{}, 0)
	for _, mp := range mapList {
		dataValue := mp[valueField]
		dataLabel := mp[labelField]

		option := make(map[string]interface{})
		option["value"] = dataValue
		option["label"] = dataLabel

		result = append(result, option)
	}

	return result
}

func (e *optionManageController) selectTreeList(context httpserver.Context, param map[string]interface{}) (interface{}, error) {
	return nil, errors.New("废弃的接口")
}
